home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Reference Guide / C-C++ Interactive Reference Guide.iso / c_ref / csource4 / 280_01 / compress.c < prev    next >
Text File  |  1989-01-13  |  2KB  |  80 lines

  1. /* [compress.c of JUGPDS Vol.46] */
  2. /*
  3. *****************************************************************
  4. *                                *
  5. *    Written by  Hakuo Katayose (JUG-CP/M No.179)        *
  6. *            49-114 Kawauchi-Sanjuunin-machi        *
  7. *            Sendai, Miyagi 980                          *
  8. *            Phone: 0222-61-3219                *
  9. *                                *
  10. *       Modifird by Toshiya Oota   (JUG-CPM No.10)              *
  11. *                   Sakae ko-po 205                 *
  12. *            5-19-6 Hosoda                *
  13. *            Katusikaku Tokyo 124            *
  14. *                                *
  15. *        for MS-DOS Lattice C V3.1J & 80186/V20/V30    *
  16. *                                *
  17. *    Compiler Option: -ccu -k0(1) -ms -n -v -w        *
  18. *                                *
  19. *    Edited & tested by Y. Monma (JUG-CP/M Disk Editor)    *
  20. *            &  T. Ota   (JUG-CP/M Sub Disk Editor)    *
  21. *                                *
  22. *****************************************************************
  23. */
  24.  
  25. /* Library functions for Software Tools */
  26.  
  27. #include "stdio.h"
  28. #include "dos.h"
  29. #include "ctype.h"
  30. #include "tools.h"
  31. #include "toolfunc.h"
  32.  
  33. #define    RCODE        127
  34. #define    MAXCHUNK    (RCODE-'0'-1)
  35. #define    THRESH        5
  36.  
  37. void    main(argc, argv)
  38. int    argc;
  39. char    *argv[];
  40.  
  41. {
  42. int    c, lastc, nrep, nsave;
  43. char    buff[MAXCHUNK];
  44. void    putbuf();
  45.  
  46.     nsave = 0;
  47.     for (lastc = getchar(); lastc != EOF; lastc = c) {
  48.         for (nrep = 1; (c = getchar()) == lastc; nrep++)
  49.             if (nrep >= MAXCHUNK)
  50.                 break;
  51.         if (nrep < THRESH)
  52.             while (nrep--) {
  53.                 buff[nsave++] = lastc;
  54.                 if (nsave >= MAXCHUNK)
  55.                     putbuf(buff, &nsave);
  56.                 }
  57.         else {
  58.             putbuf(buff, &nsave);
  59.             putchar(RCODE);
  60.             putchar(lastc);
  61.             putchar(nrep+'0');
  62.              }
  63.     }
  64.     putbuf(buff, &nsave);
  65. }
  66.  
  67.  
  68. void    putbuf(p, nsave)
  69. char    *p;
  70. int    *nsave;
  71.  
  72. {
  73.     if (*nsave) {
  74.         putchar(*nsave + '0');
  75.         while ((*nsave)--)
  76.             putchar(*p++);
  77.     }
  78.     *nsave = 0;
  79. }
  80.